//
//  SelectAreaViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/14.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "SelectAreaViewController.h"

#import "AreaLeftTableCell.h"
#import "AreaRightTableCell.h"
@interface SelectAreaViewController ()
@property (nonatomic, strong) SelectedAreaViewModel *selectedAreaViewModel;

@property (weak, nonatomic) IBOutlet UITableView *mainLeftTableView;

@property (weak, nonatomic) IBOutlet UITableView *mainRightTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *top_line_layout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *top_line_height_layout;

@end

@implementation SelectAreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftCloseBtnItem];

    self.top_line_layout.constant = 0.45;
    self.top_line_height_layout.constant = 0.45;
    
    self.selectedAreaViewModel = [[SelectedAreaViewModel alloc ] init];
    [self.selectedAreaViewModel loadData:^(NSInteger resultType, id data, NSString *errorMsg) {

        
        if (resultType == 1) {
            [self.mainLeftTableView reloadData];
            [self.mainRightTableView reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg];
        }
     
        
    }];
    
    [self.mainLeftTableView reloadData];
    [self.mainRightTableView reloadData];
    
    [self setExtraCellLineHidden:self.mainLeftTableView];
    [self setExtraCellLineHidden:self.mainRightTableView];
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.mainLeftTableView) {
        return self.selectedAreaViewModel.resultList.count;
    }else{
        if (self.selectedAreaViewModel.resultList.count  == 0) {
            return 0;
        }
        SelectedAreaModel *subLists = nil;
        for (SelectedAreaModel *subList in self.selectedAreaViewModel.resultList) {
            if (subList.CityId == self.selectedAreaViewModel.selectedLeftKey) {
                subLists = subList;
            }
        }
        
        if (subLists == nil) {
            return 0;
        }
        
        return subLists.areaList.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.mainLeftTableView) {
        AreaLeftTableCell *leftCell = [tableView dequeueReusableCellWithIdentifier:@"AreaLeftTableCell"];
        
        SelectedAreaModel *subList = [self.selectedAreaViewModel.resultList objectAtIndex:indexPath.row];
        leftCell.leftText.text = subList.CityName;
        
        if (subList.CityId == self.selectedAreaViewModel.selectedLeftKey) {
            [leftCell setBackgroundColor:[UIColor whiteColor]];
        }else{
            [leftCell setBackgroundColor:GLOBLE_BACKGROUND_COLOR];
        }
        return leftCell;
    }else{
        AreaRightTableCell *rightCell = [tableView dequeueReusableCellWithIdentifier:@"AreaRightTableCell"];
        
        SelectedAreaModel *subLists = nil;
        for (SelectedAreaModel *subList in self.selectedAreaViewModel.resultList) {
            if (subList.CityId == self.selectedAreaViewModel.selectedLeftKey) {
                subLists = subList;
            }
        }
        
        AreaEntity *entity = [subLists.areaList objectAtIndex:indexPath.row];
        rightCell.rightText.text =entity.AreaName;
        return rightCell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.mainLeftTableView) {
        
      SelectedAreaModel *subList = [self.selectedAreaViewModel.resultList objectAtIndex:indexPath.row];
        
       self.selectedAreaViewModel.selectedLeftKey =  subList.CityId;
       [self.mainLeftTableView reloadData];
       [self.mainRightTableView reloadData];
    }else{
        
        if ([self.delegate respondsToSelector:@selector(didSelectedArea:right:)]) {
            SelectedAreaModel *subLists = nil;
            for (SelectedAreaModel *subList in self.selectedAreaViewModel.resultList) {
                if (subList.CityId == self.selectedAreaViewModel.selectedLeftKey) {
                    subLists = subList;
                }
            }
            
            
            AreaEntity *entity = [subLists.areaList objectAtIndex:indexPath.row];
            
            [self.delegate didSelectedArea:subLists right:entity];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
